package com.malljelly.study.controller;

import com.alibaba.fastjson.JSONObject;
import com.malljelly.study.dao.po.PaymentInfo;
import com.malljelly.study.service.UnionPayService;
import com.malljelly.study.vo.unionpay.service.UnionPaymentInfoRequestVo;
import com.malljelly.study.vo.unionpay.service.UnionPaymentInfoResponseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by 骑着蜗牛去旅行 on 2018/8/14.
 */
@Api("银联支付接口文档")
@RestController  //@Controller+@ResponseBody
@RequestMapping("soa/unionPayService")
@Slf4j
public class UnionPayController  {
    @Autowired
    private UnionPayService unionPayService;

    @ApiOperation(value = "到支付页面")
    @GetMapping("/payIndex")
    public void payGateway(HttpServletRequest request, String token, HttpServletResponse resp) throws IOException {
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();

        try {
            //1使用token查找支付信息
            UnionPaymentInfoResponseVo payInfo = unionPayService.getPayInfoByToken(token);
            if (payInfo == null) {
                resp.getWriter().write("系统错误!");
                return;
            }

            //2返回支付html页面
            String html = unionPayService.pay(payInfo);
            out.println(html);
            out.close();
        } catch (Exception e) {
            log.error("系统错误");
            e.printStackTrace();
        } finally {
            out.close();
        }
    }


    /**
     * 1.创建支付请求token
     * 2.使用token查找支付信息
     * 3.使用订单号查询支付信息
     * 4.修改订单状态
     */

    @ApiOperation(value = "创建支付请求token", response = UnionPaymentInfoRequestVo.class)
    @PostMapping("/addPayInfoToken")
    public com.alibaba.fastjson.JSONObject addPayInfoToken(@RequestBody UnionPaymentInfoRequestVo paymentInfo) {
        JSONObject result = new JSONObject();
        try {
            String token = unionPayService.addPayInfoToken(paymentInfo);
            result.put("code", 0);
            result.put("msg", "成功");
            result.put("data", token);
        } catch (Exception e) {
            //断言异常(自定义异常)
            if (e instanceof IllegalArgumentException) {
                result.put("msg", e.getMessage());
            } else {
                result.put("msg", "系统异常");
            }
        }
        return result;
    }

    @ApiOperation(value = "使用token查找支付信息")
    @GetMapping("/getPayInfoByToken/{token}")
    public com.alibaba.fastjson.JSONObject getPayInfoByToken(@PathVariable("token") String token) {
        JSONObject result = new JSONObject();
        try {
            UnionPaymentInfoResponseVo payInfo = unionPayService.getPayInfoByToken(token);
            result.put("code", 0);
            result.put("msg", "成功");
            result.put("data", token);
        } catch (Exception e) {
            //断言异常(自定义异常)
            if (e instanceof IllegalArgumentException) {
                result.put("msg", e.getMessage());
            } else {
                result.put("msg", "系统异常");
            }
        }
        return result;
    }


    @ApiOperation(value = "使用订单号查询支付信息")
    @GetMapping("/getByOrderIdPayInfo/{orderId}")
    public com.alibaba.fastjson.JSONObject getByOrderIdPayInfo(@PathVariable("orderId") String orderId) {
        JSONObject result = new JSONObject();
        try {
            UnionPaymentInfoResponseVo payInfo = unionPayService.getByOrderIdPayInfo(orderId);
            result.put("code", 0);
            result.put("msg", "成功");
            result.put("data", payInfo);
        } catch (Exception e) {
            //断言异常(自定义异常)
            if (e instanceof IllegalArgumentException) {
                result.put("msg", e.getMessage());
            } else {
                result.put("msg", "系统异常");
            }
        }
        return result;
    }


    @ApiOperation(value = "修改订单状态", response = UnionPaymentInfoRequestVo.class)
    @PostMapping("/updatePayInfo")
    public com.alibaba.fastjson.JSONObject updatePayInfo(@RequestBody UnionPaymentInfoRequestVo paymentInfo) {
        JSONObject result = new JSONObject();
        try {
            Boolean stamp = unionPayService.updatePayInfo(paymentInfo);
            result.put("code", 0);
            result.put("msg", "成功");
            result.put("data", stamp);
        } catch (Exception e) {
            //断言异常(自定义异常)
            if (e instanceof IllegalArgumentException) {
                result.put("msg", e.getMessage());
            } else {
                result.put("msg", "系统异常");
            }
        }
        return result;
    }


}
